package com.cqeec.gams.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cqeec.gams.entity.FunctionalModule;
import com.cqeec.gams.mapper.FunctionalModuleMapper;
import com.cqeec.gams.model.Function;
import com.cqeec.gams.service.FunctionalModuleService;

/**
 * 版权所有：2020，TLF工作室。
 */

/**
 * @author 唐礼飞
 * @date 2020-06-04 10:53:35.391
 */
@Service
public class FunctionalModuleServiceImpl implements FunctionalModuleService {
	@Autowired
	private FunctionalModuleMapper functionalModuleMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cqeec.gams.service.FunctionalModuleService#getAll()
	 */
	@Override
	public List<FunctionalModule> getAll() {
		return functionalModuleMapper.findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cqeec.gams.service.FunctionalModuleService#getAllFunction()
	 */
	@Override
	public List<Function> getAllFunction() {
		List<FunctionalModule> list = this.getAll();
		List<Function> functionList = new ArrayList<>();
		// 读取功能组
		Function fun;
		Map<String, List<FunctionalModule>> map = new HashMap<String, List<FunctionalModule>>();
		for (FunctionalModule fm : list) {
			if (fm.getParent() == null) {
				fun = new Function();
				fun.setId(fm.getId());
				fun.setName(fm.getName());
				functionList.add(fun);
				map.put(fm.getId(), new ArrayList<FunctionalModule>());
			}
		}
		// 读取功能组的功能模块
		String parent;
		List<FunctionalModule> temp;
		for (FunctionalModule fm : list) {
			parent = fm.getParent();
			if (parent != null && map.containsKey(parent)) {
				temp = map.get(parent);
				temp.add(fm);
			}
		}
		// 将功能模块放到列表中
		Set<String> key = map.keySet();
		for (String k : key) {
			for (Function f : functionList) {
				if (f.getId().equals(k)) {
					f.setFunctionalModule(map.get(k));
				}
			}
		}
		return functionList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cqeec.gams.service.FunctionalModuleService#changeDisabled(java.lang.
	 * String, boolean)
	 */
	@Override
	public void changeDisabled(String id, boolean disabled) {
		functionalModuleMapper.changeDisabled(id, disabled);
	}
}
